<?php
// Display a dropdown menu of upcoming meet names, with date. Only lists events upcoming (event start greater than current time)
function createMeetDropdown () {
	$query = "SELECT meetID, name, date, location FROM meets WHERE date > '" . date("Y-m-d") ."'";
	$result = mysql_query($query) or die("Could not get details from table: meets." . mysql_error());

	echo '<select onchange="window.location=\'index.php?selectedEvent=\'+this.value">';
	
	echo '<option value="0">--- Please select a meet below ---</option>';
	
	while ( $row = mysql_fetch_array($result) ) {

	$dateString = $row['date'] . "";
	
	echo '
		<option ';
		 if ($_GET['selectedEvent'] == $row['meetID']) { echo 'selected '; }
		 echo 'name="selectedEvent" value="'.$row['meetID'].'">'.$row['name'].' - taking place on ' .  $dateString . '</option>
		';		
	
	}
	echo '</select>';
}


// Display events for this meet.
function displayEventsForMeet ($meetID) {
	$query = "SELECT * FROM ls_events WHERE meetID = $meetID";
	$result = mysql_query($query);
	
	// get the children for this person - so they can sign them up for events
	$parent = $_SESSION['ls_userid'];

	if ($result) { 
		while ( $row = mysql_fetch_array($result) ) {
		
			echo '
			<hr>
			<table>
			<tr><td>Name:</td><td>'.$row['name'].'</td></tr>
			<tr><td>Age Group:</td><td>'.$row['ageGroup'].'</td></tr>
			<tr><td>Gender:</td><td>'.$row['gender'].'</td></tr>
			<tr><td>Event Type:</td><td>'.$row['eventType'].'</td></tr>
			<tr><td>Event Length:</td><td>'.$row['eventLength'].'</td></tr>
			<tr><td>Event Date:</td><td>'.$row['eventDate'].'</td></tr>
			</table>
			<a href=\'signup.php?eventID='.$row['eventID'].'\' onclick=\'return confirmation();\'>Sign up for this event!</a>
			';	

            $child_sql = mysql_query("SELECT * FROM parentChild where parentID = '$parent'") or die(mysql_error());	
            $children = mysql_num_rows($child_sql);
			if( $children > 0 ) {
				while( $relation = mysql_fetch_assoc($child_sql) ) {
					$child = mysql_query("SELECT * FROM accounts WHERE userID = '$relation[childID]'");
					$child = mysql_fetch_assoc($child);
					echo '<br><a href=\'signup.php?childID='.$relation[childID].'&eventID='.$row['eventID'].'\' onclick=\'return confirmation();\'>Sign up '
					     . $child[name] . ' for this event!</a>';
				}
			}	
		}
	} else {
		echo "<p>There are currently no events scheduled for the meet you selected.</p>"; 
	}
}


// Display event information.
function displayEventInfo ($eventID) {
	$query = "SELECT * FROM ls_events WHERE eventID = $eventID";
	$result = mysql_query($query) or die("Could not get info for given eventID. " . mysql_error());

	while ( $row = mysql_fetch_array($result) ) {
		
			echo '
			<table>
			<tr><td>Name:</td><td>'.$row['name'].'</td></tr>
			<tr><td>Age Group:</td><td>'.$row['ageGroup'].'</td></tr>
			<tr><td>Gender:</td><td>'.$row['gender'].'</td></tr>
			<tr><td>Event Type:</td><td>'.$row['eventType'].'</td></tr>
			<tr><td>Event Length:</td><td>'.$row['eventLength'].'</td></tr>
			<tr><td>Event Date:</td><td>'.$row['eventDate'].'</td></tr>
			</table>
			';		
		}
}




// Register user into event.
// optional value to indicate it it is for their child
function registerSwimmerToEvent ($swimmerID, $eventID, $child) {
	$query = "INSERT INTO eventMembers VALUES ('$eventID', '$swimmerID')";
	$result = mysql_query($query);

	if(!$result) { 
        if( $child ) {
            $sql = mysql_query("SELECT * FROM accounts WHERE userID = '$swimmerID'") or die(mysql_error());
            $child = mysql_fetch_assoc($sql);
            echo "<p>You have already signed <b>$child[name]</b> up for this event!</p>\n<hr>";
        } else {
            echo "<p>You are already signed up for this event!</p>\n<hr>";
        }
		displayEventInfo($eventID);
	} else {
        if( $child ) {
            $sql = mysql_query("SELECT * FROM accounts WHERE userID = '$swimmerID'") or die(mysql_error());
            $child = mysql_fetch_assoc($sql);
            echo "<p>You have signed up <b>$child[name]</b> for this event:</p>\n<hr>";
        } else {
            echo "<p>You have been signed up for the event you selected:</p>\n<hr>";
        }
		displayEventInfo($eventID);
            
        $event = mysql_query("SELECT * FROM ls_events WHERE eventID = '$eventID'") or die(mysql_error());
        $event = mysql_fetch_assoc($event);
        $sql = mysql_query("INSERT INTO meetsBilling (userID, meetID, status) VALUES ('$swimmerID', '$event[meetID]', 'UNPAID')");
    }
	echo "<hr>\n<a href=index.php>Back to Event Signup page</a>";

}


// Delete user from event.
function deleteSwimmerFromEvent ($swimmerID, $eventID) {
	$query = "DELETE FROM eventMembers WHERE swimmerID = '$swimmerID' AND eventID = '$eventID'";
	$result = mysql_query($query);

	if(!$result) { 
		echo "<p>Could not remove you from the event." . mysql_error() . "</p>\n<hr>";
		displayEventInfo($eventID);
	} else {
		echo "<p>You have been removed from the event you selected:</p>\n<hr>";
		displayEventInfo($eventID);
	}
	
	echo "<hr>\n<a href=index.php>Back to Event Signup page</a>";
}


// Get all the events the user is registered in and return an array
function usersRegisteredEvents ($userID) {
	$query = "SELECT eventID FROM eventMembers WHERE swimmerID = $userID";
	$result = mysql_query($query) or die("Could not get details from table: eventMembers." . mysql_error());

	$eventArray = array();
	
	while ( $row = mysql_fetch_array($result) ) {
	
		array_push($eventArray, $row['eventID']);		
	
	}

	return $eventArray;
}



// Get the past events the user has registered in/attended and return an array of eventIDs
function usersPastEvents ($userID) {
	$query = "SELECT eventID FROM ls_events WHERE eventDate < '" . date("Y-m-d") . "'";
	$result = mysql_query($query) or die("Could not get details from table: ls_events." . mysql_error());

	$allEventsUsersRegistered = array();
	$allEventsUsersRegistered = usersRegisteredEvents($userID);
	
	// Keep the event in the array if the event is in the future, otherwise remove it.
	
	$eventArray = array();
	
	while ( $row = mysql_fetch_array($result) ) {
		if(in_array($row['eventID'], $allEventsUsersRegistered) ) {
			array_push($eventArray, $row['eventID']);
		}
	
	}

	return $eventArray;
}


// Get the upcoming events the user is registered in and return an array of eventIDs
function usersUpcomingEvents ($userID) {
	$query = "SELECT eventID FROM ls_events WHERE eventDate > '" . date("Y-m-d") . "'";
	$result = mysql_query($query) or die("Could not get details from table: ls_events." . mysql_error());

	$allEventsUsersRegistered = array();
	$allEventsUsersRegistered = usersRegisteredEvents($userID);
	
	// Keep the event in the array if the event is in the future, otherwise remove it.
	
	$eventArray = array();
	
	while ( $row = mysql_fetch_array($result) ) {
		if(in_array($row['eventID'], $allEventsUsersRegistered) ) {
			array_push($eventArray, $row['eventID']);
		}
	
	}

	return $eventArray;
}


?>